//
//  KLDPDetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/9/2.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "KLDPDetViewController.h"

#import "GXDPView.h"
#import "PinJiaCell.h"
#import "PingJiaViewController.h"
#import "KuLiViewController.h"
@interface KLDPDetViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSMutableArray *array;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation KLDPDetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self showNav:@"店铺详情"];
    array = [NSMutableArray array];
    [self loadUI];
    [self loadData2];
    //NSLog(@"images:%@",_Imgdatas);
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-60) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    //头视图
    GXDPView *view = [[[NSBundle mainBundle] loadNibNamed:@"GXDPView" owner:nil options:nil] lastObject];
    view.frame = CGRectMake(0, 0, ScreenWidth, 290);
    _tableView.tableHeaderView = view;
    
    view.labName.text = _name;
    view.labdet.text = _det;
    view.labPlace.text = [NSString stringWithFormat:@"地址：%@",_place];
    view.labOrder.text = _order;
    
    UILabel *labs = [[UILabel alloc] initWithFrame:CGRectMake(0, 280, ScreenWidth, 10)];
    labs.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [view addSubview:labs];
    
    //尾
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-60, ScreenWidth, 60)];
    footView.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self.view addSubview:footView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(ScreenWidth/3*2, 10, ScreenWidth/3, 50);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"呼叫苦力" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickTEL) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:btn];
    
    UIView *views = [[UIView alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth/3*2, 50)];
    views.backgroundColor = [UIColor whiteColor];
    [footView addSubview:views];
    
    UILabel *lab1 = [[UILabel alloc] init];
    lab1.text = _name;
    lab1.font = [UIFont systemFontOfSize:13.0];
    [views addSubview:lab1];
    [lab1 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(views.left).offset(16);
        make.top.equalTo(views.top).offset(8);
    }];
    
    UILabel *lab2 = [[UILabel alloc] init];
    lab2.text = _place;
    lab2.textColor = [UIColor darkGrayColor];
    lab2.font = [UIFont systemFontOfSize:11.0];
    [views addSubview:lab2];
    [lab2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(views.left).offset(16);
        make.top.equalTo(lab1.bottom).offset(8);
    }];
    
}
- (void)clickTEL{
    KuLiViewController *kuli = [[KuLiViewController alloc] init];
    [self.navigationController pushViewController:kuli animated:YES];
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    PinJiaCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"PinJiaCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (array.count == 0) {
        return cell;
    }
    
    NSDictionary *dic = array[indexPath.row];
    cell.labName.text = [dic objectForKey:@"nickname"];
    cell.labDate.text = [dic objectForKey:@"commenttime"];
    cell.labText.text = [dic objectForKey:@"comment"];
    NSString *img = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"avatar"]];
    [cell.images sd_setImageWithURL:[NSURL URLWithString:img] placeholderImage:[UIImage imageNamed:@"TIM图片20170810135854"]];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 100, 20)];
    NSString *count = [NSString stringWithFormat:@"总评价：%li",array.count];
    lab.text = count;
    lab.font = [UIFont systemFontOfSize:14.0];
    [view addSubview:lab];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(16, 39, ScreenWidth, 0.5)];
    line.backgroundColor = [UIColor grayColor];
    [view addSubview:line];
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}

//尾视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, ScreenWidth, 40.0);
    [btn setTitle:@"查看全部评价" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2+35, 14, 60, 12)];
    NSString *count = [NSString stringWithFormat:@" (%li)",array.count];
    lab.text = count;
    lab.font = [UIFont systemFontOfSize:12.0];
    lab.textColor = [UIColor redColor];
    [view addSubview:lab];
    
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40.0;
}

- (void)click{
    PingJiaViewController *pingjia = [[PingJiaViewController alloc] init];
    pingjia.merid = _merid;

    [self.navigationController pushViewController:pingjia animated:YES];
}
//苦力评论
- (void)loadData2{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    [parmas setObject:@"1" forKey:@"p"];
    NSLog(@"parmas:%@",parmas);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Merchantcomment/coolie/merchantid/11",urlHead];
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"苦力评论res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            array = [[responseObject objectForKey:@"result"] objectForKey:@"comment"];
        }
        
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [_tableView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
